package com.cuicuisha.gxyp.manager.controller;

import com.cuicuisha.gxyp.manager.service.SysMenuService;
import com.cuicuisha.gxyp.manager.service.SysUserService;
import com.cuicuisha.gxyp.manager.service.ValidateCodeService;
import com.cuicuisha.gxyp.model.dto.system.LoginDto;
import com.cuicuisha.gxyp.model.entity.system.SysUser;
import com.cuicuisha.gxyp.model.entity.system.SysUserAuthContextUtil;
import com.cuicuisha.gxyp.model.vo.common.Result;
import com.cuicuisha.gxyp.model.vo.common.ResultCodeEnum;
import com.cuicuisha.gxyp.model.vo.system.LoginVo;
import com.cuicuisha.gxyp.model.vo.system.SysMenuVo;
import com.cuicuisha.gxyp.model.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: IndexController
 * Description:
 *
 * @Author 脆脆鲨
 * @Create 2024-09-09 20:08
 * @Version 1.0
 */

@RestController
@RequestMapping("/admin/system/index")
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private ValidateCodeService validateCodeService;

    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/getUserInfo")
    public Result<SysUser> getUserInfo() {
        return Result.build(SysUserAuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }
}
